package com.sinoglobal.sinostore.activity;

import java.util.ArrayList;

import andbase.listener.AbOnListViewListener;
import andbase.view.pullview.AbPullListView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.BaseActivity;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.OrderAdapter;
import com.sinoglobal.sinostore.bean.OrderListVo;
import com.sinoglobal.sinostore.bean.OrderVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.utils.TextUtil;

/**
 * 
 * @title: OrderListActivity.java 
 * @package com.chiliring.sinostore.activity 
 * @description: 订单列表
 * @author zrh
 * @date 2015年1月12日 下午8:02:58
 */
public class OrderListActivity extends StateBaseActivity implements AbOnListViewListener{

	AbPullListView lvOrder;
	View noOrderView;
	View goToMall;

	OrderAdapter adapter;
	int pageNum=1;
	HttpUtils http = new HttpUtils();
	ProgressDialog progressDialog;
	int selectedPosition;
	public static Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_order_list);
		openChenJInShi();
		titleView.setText("我的订单");
		lvOrder=getViewById(R.id.lvOrder);
		noOrderView=getViewById(R.id.noOrder);
		goToMall=getViewById(R.id.btnGoToMall);
		
		adapter=new OrderAdapter(this, new ArrayList<OrderVo>());
		lvOrder.setAdapter(adapter);
		lvOrder.setEmptyView(noOrderView);
		noOrderView.setVisibility(View.GONE);
		lvOrder.setAbOnListViewListener(this);
		lvOrder.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (position>0) {
					selectedPosition=position-1;
					Intent intent=new Intent(OrderListActivity.this,OrderDetailActivity.class);
					intent.putExtra(OrderVo.class.getCanonicalName(), adapter.getItem(selectedPosition));
					startActivityForResult(intent,1);
				}
			}
		});
		goToMall.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toActivity(FirstActivity.class, null);
				OrderListActivity.this.finish();
			}
		});
		progressDialog=new ProgressDialog(this);
		handler=new Handler(){
			public void handleMessage(android.os.Message msg) {
				if (-1==msg.what) {
					adapter.removeItem(selectedPosition);
				}else{
					adapter.getItem(selectedPosition).setStatus(String.valueOf(msg.what));
					adapter.notifyDataSetChanged();
				}
			};
		};
		loadData();
	}
	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		onRefresh();
	}
	private void loadData() {
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "200");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("user_id", Constants.userCenterId);
		params.addQueryStringParameter("pageSize","10");
		params.addQueryStringParameter("draw_type","");
		params.addQueryStringParameter("page",String.valueOf(pageNum));
        ApiDebugUtil.debug(params);
		http.send(HttpRequest.HttpMethod.POST,
				Constants.STORE_URL,
				params,
				new RequestCallBack<String>() {

			@Override
			public void onStart() {
				try {
					progressDialog.show();
				} catch (Exception e) {
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				lvOrder.stopLoadMore();
				lvOrder.stopRefresh();
				if (responseInfo!=null&&TextUtil.stringIsNotNull(responseInfo.result)) {
					LogUtils.e("responseInfo.result==>"+responseInfo.result);
					OrderListVo orderListVo=null;
					try {
						orderListVo = JSON.parseObject(responseInfo.result, OrderListVo.class);
					} catch (Exception e1) {
						e1.printStackTrace();
						showShortToast("数据解析出错了");
						return;
					}
					Constants.PIC_HOST = orderListVo.getHost();
					LogUtils.e("code=="+orderListVo.getCode());
					noOrderView.setVisibility(View.VISIBLE);
					if (!"0".equals(orderListVo.getCode())) {
						Toast.makeText(OrderListActivity.this, orderListVo.getMessage(), 0).show();
						lvOrder.setPullLoadEnable(false);
						return;
					}
					if (pageNum==1) {
						adapter.clearData();
					}
					adapter.addData(orderListVo.getResult());
					pageNum++;
					int totalPage=1;
					try {
						totalPage = Integer.parseInt(orderListVo.getPageNum());
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (pageNum>totalPage) {
						lvOrder.setPullLoadEnable(false);
					}else {
						lvOrder.setPullLoadEnable(true);
					}
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				LogUtils.e("errorcode=="+error.getExceptionCode()+",msg=="+msg);
			}
		});

	} 
	@Override
	public void onRefresh() {
		pageNum=1;
		loadData();

	}
	@Override
	public void onLoadMore() {
		loadData();
	}
    
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		switch (arg0) {
		case 0://评论页面 返回
			if(arg1==RESULT_OK){
				pageNum=1;
				loadData();
			}
			break;
		default:
			break;
		}
	}
}
